package cn.tedu.csmall.passport.web;



import cn.tedu.csmall.passport.ex.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class JsonResult {
    /**
     * 状态码
     */
    private Integer state;
    /**
     * 操作“失败”时的描述文本
     */
    private String message;
    /**
     * 操作“成功”时的数据
     */
    private Object data;

    public static JsonResult ok(){
        return ok(null);
    }
    public static JsonResult ok(Object data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult fail(ServiceCode serviceCode, String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(serviceCode.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }
}
